//
//  PictureManageViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/5/2.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define kItemWidth (App_Frame_Width - (15*5))/4

#import "PictureManageViewController.h"

#import "BHSelectMediaView.h"
#import "UIViewController+BHMediaExt.h"
#import "TZImagePickerController.h"

@interface PictureManageViewController () <PublicWelfareImageViewDataSource,TZImagePickerControllerDelegate>

@property (nonatomic, strong) BHSelectMediaView *mediaView;
@property (nonatomic, strong) NSMutableArray *imageArray;//实际数组
@property (nonatomic, strong) NSMutableArray *assetsArray;//记录临时数组

@property (nonatomic, assign) BOOL isEdit;
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation PictureManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"公司图片管理";
    [self initRightBtn];
    [self initNavBarBackBtn];
    [self setUpUI];
    
    self.imageArray = [[NSMutableArray alloc] initWithCapacity:5];
    self.assetsArray = [[NSMutableArray alloc] initWithCapacity:5];
    
    self.isEdit = NO;
}

- (void)initRightBtn {
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_rightButton setFrame:CGRectMake(0, 0, 60, 44)];
//    [rightButton setImage:[UIImage imageNamed:@"my-2"] forState:UIControlStateNormal];
    [_rightButton setTitle:@"管理" forState:UIControlStateNormal];
    [_rightButton setTitleColor:Text_Color_000000 forState:UIControlStateNormal];
    [_rightButton setBackgroundColor:[UIColor clearColor]];
    [_rightButton addTarget:self action:@selector(editAction) forControlEvents:UIControlEventTouchUpInside];
    [_rightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)editAction {
    self.isEdit = !self.isEdit;
    if (self.imageArray.count != 0) {
//        self.mediaView.isDisplayDelete = self.isEdit;
        self.mediaView.isImplement = !self.isEdit;
        [self.mediaView reloadDataSelectMediaView];
    }
    else {
        self.isEdit = NO;
        self.mediaView.isImplement = YES;
        [self.mediaView reloadDataSelectMediaView];
    }
    
    if (self.isEdit == YES) {
        [_rightButton setTitle:@"完成" forState:UIControlStateNormal];
    } else {
        [_rightButton setTitle:@"管理" forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpUI {
    self.mediaView = [[BHSelectMediaView alloc]init];
    self.mediaView.dataSource = self;
    self.mediaView.maxImageCount = 20;
    self.mediaView.isDisplayDelete = YES;
    self.mediaView.isImplement = YES;
    self.mediaView.isNormal = YES;
    [self.view addSubview:self.mediaView];
    
    [self.mediaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
//        make.left.equalTo(self.view);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(10);
//        make.height.offset(BHCommunity_ImageHeight);
        make.bottom.equalTo(self.view).offset(-10);
    }];
    
}

#pragma mark - PublicWelfareImageViewDataSource
- (CGSize)collectionView:(UICollectionView *)collectionView sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kItemWidth, kItemWidth);
}

- (NSArray *)dataArraynumberOfItems:(BHSelectMediaView *)mediaView {
    return self.imageArray;
}

- (void)deleteSelectMediaView:(BHSelectMediaView *)mediaView deleteItem:(NSInteger)item {
    [self.imageArray removeObjectAtIndex:item];
    [self.assetsArray removeObjectAtIndex:item];
    [self _changeViewFormImageCount];
}

- (void)deleteAddImage:(BHSelectMediaView *)mediaView {
    [self _selectPictureCamera];
}

#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    [self.imageArray removeAllObjects];
    [self.assetsArray removeAllObjects];
    [self.imageArray addObjectsFromArray:photos];
    [self.assetsArray addObjectsFromArray:assets];
    [self _changeViewFormImageCount];
}


#pragma mark -
- (void)_selectPictureCamera {
    [UIViewController showAlertWithTitle:nil message:nil actionTitles:@[@"相册"] cancelTitle:@"取消" style:UIAlertControllerStyleActionSheet completion:^(NSInteger index) {
        if (index == 0) {
            TZImagePickerController *imagePickController = [[TZImagePickerController alloc] initWithMaxImagesCount:20 delegate:self];
            imagePickController.allowPickingVideo = NO;
            imagePickController.selectedAssets = self.assetsArray;
            [self presentViewController:imagePickController animated:YES completion:nil];
        }
    }];
}

- (void)_changeViewFormImageCount {
//    [UIView animateWithDuration:0.25 animations:^{
//        self.backView.frame = CGRectMake(0, 0, App_Frame_Width, 250 + ((self.imageArray.count < 3) ? 0 : Image_Height));
//        self.mediaView.frame = CGRectMake(15, CGRectGetMaxY(self.textView.frame) + 10, App_Frame_Width - 30, (self.imageArray.count >= 3) ?  2 * Image_Height + 2 : Image_Height);
//        if (self.channelType == ProjectsCircleChannelTypeTransact) {
//            self.purchasingLinkView.frame = CGRectMake(0, CGRectGetMaxY(self.backView.frame), App_Frame_Width, 85);
//        }
//        self.addressView.frame = CGRectMake(0, CGRectGetMaxY(self.channelType == ProjectsCircleChannelTypeTransact ? self.purchasingLinkView.frame : self. backView.frame) + 10, App_Frame_Width, 45);
//        if (self.channelType == ProjectsCircleChannelTypeTransact) {
//            self.ruleLabel.frame = CGRectMake(15, CGRectGetMaxY(self.addressView.frame), App_Frame_Width - 30, 30);
//        }
//    } completion:^(BOOL finished) {
        [self.mediaView reloadDataSelectMediaView];
//    }];
}

//- (void)_loadData:(id)response Refresh:(BOOL)refresh {
////    if (refresh) [self.imageArray removeAllObjects];
////    NSArray *arr = [NSArray yy_modelArrayWithClass:[HomeShowListModel class] json:response];
////    [self.dataArray addObjectsFromArray:arr];
////    if (arr.count < 10) {
////        [self.tableView endRefreshingWithNoMoreData];
////    }
////    [self.tableView reloadData];
//}
//
//- (void)requestPostBH_Exhibition_getExhibitionByHallId:(BOOL)refresh PageCount:(NSInteger)pageCount {
//    SGUserModel *userModel = [SGUserInfo getUserInfo];
//
//    self.imageArray.count != 0 ?: [[BHAlertUtil alertManager]showLoadingView:self.view];
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
////    [params setObject:@"" forKey:@"uid"];
//    [params setObject:userModel.company_id forKey:@"company_id"];
//    [params setObject:[NSString stringWithFormat:@"%ld",(long)pageCount] forKey:@"page"];
//    [params setObject:@"10" forKey:@"pagesize"];
//
//    [PPHTTPRequest requestPostBH_User_getCompanyPic:^(id response, BOOL success) {
//        if (self.dataArray.count == 0 && [response isKindOfClass:[NSString class]]) {
//            //            self.collectionView.placeholderShow(YES).prompt_title((NSString *)response).isButtonHidden(YES);
//        }else self.mediaView.placeholderShow(NO);
//        if (success && ![response isKindOfClass:[NSString class]]) {
//            [self _loadData:response Refresh:refresh];
//        }
//        [[BHAlertUtil alertManager]hiddenLoading];
//    } params:params];
//}

@end
